package com.bloody.android.Bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.bloody.android.R;
import com.bloody.android.Map.Map;
import com.bloody.android.Observer.Observer;

public class Create extends MBT implements Observer{
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.create);
        
        ensureDiscoverable();
	}
	
	private void ensureDiscoverable() {
        if (super.getmBluetoothAdapter().getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        	int time = 300;
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, time);
            startActivity(discoverableIntent);
            Toast.makeText(getApplicationContext(), "Your device is discoverable for " + Integer.toString(time), 
            		Toast.LENGTH_SHORT).show();
        }
    }

	/*public void connected(){
		Map map = new Map(20,20);
    	//Utility.mapSaving(super.getBundle(), map);
		super.getBundle().putSerializable("map", map);
    	super.getBundle().putInt("player", 1);
    	super.getmService().write("map", map.toString());
    	
    	Bundle bundle = new Bundle();
    	bundle.putBoolean("create", true);
    	
    	super.characterSelection(bundle);
	}*/
	
	public void setCharacters(Bundle bundle){
		super.setCharacters(bundle);
		super.launchBcg();
	}
	
	@Override
	public void update(Bundle bundle) {
		super.update(bundle);
		if(bundle.getString("type").equalsIgnoreCase("connected"))
		{
			Map map = new Map(20,20);
			super.getBundle().putSerializable("map", map);
	    	super.getBundle().putInt("player", 1);
	    	BluetoothService.getInstance(null).write("map", map.toString());
	    	
	    	Bundle info = new Bundle();
	    	info.putBoolean("create", true);
	    	
	    	super.characterSelection(info);
		}
	}
}
